/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wikitrafic.iovanalex.ro;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class TraceLogParser {

    private String logPath;

    public TraceLogParser(String logPath) {
        this.logPath = logPath;

    }

    public TraceLog parse() {
        ArrayList<TraceItem> traces = new ArrayList<TraceItem>();
        String uid="";
        String date="";
        try {
            FileInputStream fstream = new FileInputStream(logPath);

            File tfile = new File(logPath);
            String fileName = tfile.getName();
//            System.out.println("Log file name is "+fileName);
            uid = fileName.split("_")[0];
            fileName = fileName.split("_")[1];
//            System.out.println("Now Log file name is "+fileName);
            date = fileName.substring(0, 8);
            System.out.println("Here"+uid+"|"+date);

            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                String[] tokens = strLine.split(" ");

                String timestamp = tokens[0];
                String lat = tokens[1];
                String lon = tokens[2];
                String satCount = tokens[3];
                String maxSat = tokens[4];
 //               System.out.println(timestamp+"|"+lat+"|"+lon+"|"+satCount+"|"+maxSat);
                traces.add(new TraceItem(timestamp, lat, lon, satCount, maxSat));
            }

            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

        return new TraceLog(uid, date, traces);
    }


}
